package com.youngwang.http;

import com.youngwang.utils.SystemConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * 使用 RestTemplate 进行 HTTP 请求
 * @author youngwang
 *         Date: 2016/9/28
 */
public class RestClient {

    private static final Logger logger = LoggerFactory.getLogger(RestClient.class);

    private static String URL = SystemConfigUtils.getProperty("Demo.hello");

    @Autowired
    private static RestTemplate template = RestTemplateUtils.getRestTemplate();

    public static String getHello(URI uri) {
        logger.debug("调用开始");
        ResponseEntity<String> result = template.getForEntity(uri,String.class);
        logger.debug("resultStatusCode : {}", result.getStatusCode());
        logger.debug("resultStatusCodeValue : {}", result.getStatusCodeValue());
        return result.getBody();
    }

    public static void main(String[] args) throws Exception{

        logger.debug("Demo.hello : {}",URL);
        URI uri = new URI(URL);
        String response = getHello(uri);
        logger.debug(response);
    }
}
